/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.velocityme.entity;

import com.velocityme.utility.ServerConfiguration;
import java.io.File;
import java.io.InputStream;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;

/**
 *
 * @author rcrida
 */
@Entity
@NamedQuery(name="findByServerName",
            query="SELECT f FROM FileAttachment f WHERE f.serverName = :serverName")
public class FileAttachment implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    private String serverName;

    private String contentType;

    private boolean compressed;

    private long length;

    @OneToOne(mappedBy = "fileAttachment")
    private ChangeDelta changeDelta;

    public FileAttachment() {
    }

    public FileAttachment(ChangeDelta changeDelta, File file, String contentType, InputStream stream) {
        this.contentType = contentType;
        this.changeDelta = changeDelta;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public java.io.File getFile() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(ServerConfiguration.getInstance().getFileAttachmentHome());
        stringBuffer.append(File.separatorChar);
        stringBuffer.append("dir");
        stringBuffer.append(Long.toString(id.longValue() / 100L));
        stringBuffer.append(File.separatorChar);
        stringBuffer.append(serverName);
        return new File(stringBuffer.toString());
    }

    public boolean isCompressed() {
        return compressed;
    }

    public String getName() {
        return name;
    }

    public String getContentType() {
        return contentType;
    }

    public long getLength() {
        return length;
    }

    public String getServerName() {
        return serverName;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof FileAttachment)) {
            return false;
        }
        FileAttachment other = (FileAttachment) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.velocityme.entity.FileAttachment[id=" + id + "]";
    }

}
